//import Vue from 'vue'
import Vue from 'vue/dist/vue.esm.js'
import App from './App.vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'

import '@/assets/app.css'
import routes from './routes.js'

import Timer from './check/Timer'
import Page from './components/Page.vue'
import Modal from './components/Modal.vue'
import Timer10 from './check/Timer10.vue'

//import home from './home/home.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI);
 
Vue.config.productionTip = false

Vue.use(VueRouter)
Vue.use(Vuex)
Vue.component("Timer", Timer);
Vue.component('page', Page);
Vue.component('modal', Modal);
Vue.component("Timer10", Timer10);

Vue.prototype.delayRun = function(fn, delayMilliseconds){
	return window.setTimeout(fn, delayMilliseconds);
}

Vue.prototype.cancleRun = function(delayRunId){
	window.clearTimeout(delayRunId);
}
  
const router = new VueRouter({
	mode:'hash',
	routes // (缩写) 相当于 routes: routes
})

const store = new Vuex.Store({
  state: {
    menuIndex: 1
  },
  mutations: {
    setMenuIndex (state, index) {
      state.menuIndex = index;
    }
  }
})
 
new Vue({
	render: h => h(App),
	router,
	store,
}).$mount('#app')


Vue.prototype.toast = function(msg, milliseconds){
	window.App.toast(msg, milliseconds);
}

//全局守卫 
router.beforeEach((to, from, next)=> {
	window.App.toast('');
	next();
})

Vue.prototype.send = function(message){
	if(!message || !message.name){
		alert("请提供有效的Message数据, 比如 {name:'select', type:'check',remark:'用户选择体检加拍照' } ");
		return;
	}
	if(!message.route)
		message.route = this.$route.path;
	//alert("SEND: " + JSON.stringify(message))
	try{
		window.chrome.webview.postMessage(message); 
	}catch(ex){
		console.log(ex)
	} 
}

Vue.prototype.sendLog = function(remark){
	this.send({name:'UserAction', remark, route:'/home'}); 
}

Vue.prototype.listen = function(fn){
	if(!fn ){
		alert("请提供有效的处理方法, 比如 (data)=>{alert(data))}");
		return;
	} 
	window.listen = fn;
}
window.listen = function(data){alert(JSON.stringify(data))};
try{
	window.chrome.webview.addEventListener('message', event => window.listen(event.data));
}catch(ex){
	console.log(ex)
}

